<template>
  <el-dialog
    :visible.sync="dialogOpen"
    :title="dialogTitle"
    :before-close="cancel"
    width="50%"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="工具名称" prop="toolName">
            <el-input v-model="formData.toolName" placeholder="请输入"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="contact">
            <el-input v-model="formData.contact" placeholder="请输入"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="工具描述" prop="remark">
            <el-input
              v-model="formData.remark"
              type="textarea"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="文件上传" prop="files">
            <el-upload
              class="upload"
              ref="upload"
              name="files"
              :action="upload.url"
              accept=".txt, .xls, .xlsx"
              :before-upload="beforeUpload"
              :on-change="handleChange"
              :on-success="handleSuccess"
              :on-remove="handleRemove"
              multiple
              :limit="5"
              :file-list="formData.files"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                请上传工具及pdf格式操作说明！
              </div>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<style scoped>
.dialog-footer {
  text-align: center;
}
</style>
<script>
import { addTool, updateTool, testConnection } from "@/api/aifactory";

export default {
  name: "Edit",
  props: [
    "dialogTitle", //标题
    "dialogOpen", //显示
    "dialogData",
  ],
  data() {
    return {
      formData: {},
      upload: {
        url: process.env.VUE_APP_BASE_API + "/file/batch/upload",
      },
      // 表单校验
      rules: {
        toolName: [
          { required: true, message: "工具名称不能为空", trigger: "blur" },
        ],
        contact: [
          { required: true, message: "负责人不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {},
  created() {},
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.formData = this.dialogData;
      console.log(119, "formdata:", this.formData);
    },
    handleChangeType(val) {
      console.log(134, val);
      for (let i in this.sourceTypeDatas) {
        if (this.sourceTypeDatas[i].dictLabel == val) {
          this.sourceConfig = this.sourceTypeDatas[i].sourceConfig;
        }
      }
      console.log("sourceConfig:", this.sourceConfig);
      if (val != "file") {
        this.isMysql = true;
      }
      console.log("ismysql:", this.isMysql);
    },
    // 取消按钮
    cancel() {
      this.$emit("closeDialog");
    },
    // 表单重置
    reset() {
      this.formData = {
        sourceName: undefined,
        sourceCode: undefined,
        sourceType: undefined,
        sourceDesc: undefined,
      };
      this.resetForm("form");
    },
    beforeUpload(file) {
      console.log('beforeUpload', file);
      const isExt = file.type === 'application/pdf' || file.type === 'application/x-msdownload';

        if (!isExt) {
          this.$message.error('请上传工具及pdf格式操作说明！');
        }
        return isExt;
    },
    handleChange(file) {
      // console.log('handleChange', file)
    },
    handleSuccess(res) {
      console.log("handleSuccess", res, this.formData);
      if (res.code == 200) {
        let files = [];
        if(this.formData?.toolFiles && JSON.parse(this.formData.toolFiles).length > 0){
          files = JSON.parse(this.formData.toolFiles)
        }
        console.log(191, files)
        files.push(res.data[0]);
        console.log("193, files", files);
        this.formData.toolFiles = JSON.stringify(files);
        console.log("toolFiles", this.formData.toolFiles);
      }
    },
    handleRemove(file){
      console.log('handleRemove', file, this.formData)
      this.formData.files.forEach((item, index) => {
        if(file.uid === item.uid){
          this.formData.files.splice(index, 1)
        }
      })
      this.formData.toolFiles = JSON.stringify(this.formData.files)
      console.log('handleRemove22', this.formData)
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        console.log("submitForm", this.formData);
        if (this.formData.sourceType != "file") {
          this.formData.sourceConfig = JSON.stringify(this.sourceConfig);
        } else {
          this.formData.sourceConfig = "{}";
        }
        console.log("new submitForm", this.formData);
        if (valid) {
          if (this.formData.id != undefined) {
            console.log(111);
            updateTool(this.formData).then((response) => {
              console.log(response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.msg, type: "error" });
              }
            });
          } else {
            addTool(this.formData).then((response) => {
              console.log(1111, response);
              if (response.code == 200) {
                this.$message({ message: "操作成功", type: "success" });
                this.$emit("closeDialog");
              } else {
                this.$message({ message: response.message, type: "error" });
              }
            });
          }
        }
      });
    },
    // 测试连接
    testConnect() {
      const data = {sourceConfig: this.formData.sourceConfig, sourceType: this.formData.sourceType}
      console.log(196, data)
      testConnection(data).then(res => {
        console.log('testConnection', res)
      })
    },
  },
};
</script>
